package com.dingding.service.account.facade;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.Useraccount;
import com.dingding.common.core.beans.Useraccountdetail;
import com.dingding.common.core.beans.Userintegral;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.account.user.service.IUserAccountService;
import com.dingding.service.account.core.dao.IUserAccountDao;
import com.dingding.service.account.core.dao.IUserAccountDetailDao;
import com.dingding.service.account.core.dao.IUserIntegralDao;

@Component("userAccountService")
public class UserAccountServiceImpl implements IUserAccountService {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private IUserAccountDao userAccountDao;

	@Autowired
	private IUserAccountDetailDao userAccountDetailDao;

	@Autowired
	private IUserIntegralDao userIntegralDao;

	@Resource(name = "userAccountSer")
	private com.dingding.service.account.core.service.IUserAccountService userAccountService;

	/**
	 * @Description:获取有用户账户
	 * @param loginId
	 *            用户登录id
	 * @author CHEHUANBO
	 * @return Useraccount
	 */
	@Override
	public Useraccount getUserAccount(String loginId) {

		return userAccountDao.getUserAccount(loginId);
	}

	/**
	 * 
	 * @Description: 扣钱
	 * @param loginId
	 *            用户ID
	 * @param money
	 *            金额
	 * @author CHEHUANBO
	 * @return boolean true 扣除成功 false 扣除失败
	 * @since V2.3.0
	 * 
	 */
	@Override
	public boolean deductRemainMoney(String loginId, BigDecimal money) {
		return userAccountService.deductRemainMoney(loginId, money);
	}


	/**
	 * @Description: 扣除剩余金钱（出租车）
	 * @param loginId 登录id
	 * @param fee 费用
	 * @author YHQ
	 * @since V2.5.4
	 * @return boolean true 扣除成功   false 扣除失败
	 */
	@Override
	public boolean taxiDeductRemainMoney(String loginId, BigDecimal money) {
		return userAccountService.taxiDeductRemainMoney(loginId, money);
	}
	
	/**
	 * 
	 * @Description: 充值
	 * @param loginId
	 *            用户ID
	 * @param money
	 *            充值金额
	 * @author CHEHUANBO
	 * @return boolean true 充值成功 false 充值失败
	 * @since V2.3.0
	 * 
	 */
	@Override
	public boolean recharge(String loginId, BigDecimal money) {
		return userAccountService.recharge(loginId, money);
	}

	/**
	 * 
	 * @Title: refunds 
	 * @Description: 给乘客退款(顺风车) 
	 * @param loginId 用户Id
	 * @param fee 退款金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	public boolean refunds(String loginId, BigDecimal fee){
		
		return userAccountService.refunds(loginId, fee);
	}
	
	
	/**
	 * 
	 * @Title: transferAccounts 
	 * @Description: 转账操作
	 * @param loginId 用户Id
	 * @param fee 转账金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	public boolean transferAccounts(String loginId, BigDecimal fee){
		
		return userAccountService.transferAccounts(loginId, fee);
	}
	
	/**
	 * @Description: 添加交易明细
	 * @param userId 用户id
	 * @param money 交易金额
	 * @param type 交易类型（1:收入 2: 支出 3:提现 4:取消）
	 * @author fengshuonan
	 * @return void
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addAccountDetail(String userId, BigDecimal tradeMoney, BigDecimal actualPaymentMoney, BigDecimal couponMoney,
			int type) {

		// 创建用户交易明细对象
		Useraccountdetail accountDetail = new Useraccountdetail();

		// 交易明细Id
		accountDetail.setUadId(ToolUtil.getUid());
		// 用户Id
		accountDetail.setUserId(userId);
		// 交易金额
		accountDetail.setTradeMoney(tradeMoney);
		// 交易类型 1:收入 2: 支出 3:提现 4:取消 5:返现
		accountDetail.setTradeType(type);
		// 交易时间
		accountDetail.setTradeTime(new Date());
		// 实际支付金额
		accountDetail.setActualPaymentMoney(actualPaymentMoney);
		// 优惠金额
		accountDetail.setCouponMoney(couponMoney);

		try {
			this.userAccountDetailDao.save(accountDetail);
		} catch (Exception e) {
			log.error("存储交易详情时候出错！UserAccountServiceImpl ： addAccountDetail()", e);
		}

	}

	/**
	 * 
	 * @Title: updateAccount 
	 * @Description: 修改用户账户信息
	 * @param useraccount 用户账户信息
	 * @author YHQ 
	 * @return void    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateAccount(Useraccount useraccount) {

		try {
			userAccountDao.update(useraccount);
		} catch (Exception e) {
			log.error("修改用户账户信息时出现异常   类：" + this.getClass() + " updateAccount()", e);
		}

	}

	/**
	 * @Description: 获取用户积分
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return Userintegral
	 * @since 2.3.0
	 */
	@Override
	@Transactional(readOnly = true)
	public Userintegral getUserIntegral(String userId) {

		Criterion criterion = Restrictions.eq("loginId", userId);

		try {
			Userintegral userintegral = this.userIntegralDao.queryCriteriaUnResult(criterion);
			return userintegral;
		} catch (Exception e) {
			log.error("获取用户详情时出现异常!" + this.getClass() + ":getUserIntegral()", e);
			return null;
		}
	}

	/**
	 * @Description 给司机账户转账
	 * @param loginId 司机的登录id
	 * @param fee 费用
	 * @author fengshuonan
	 * @return boolean
	 */
	@Override
	public boolean transferAccountsDriver(String loginId, BigDecimal fee) {
		return userAccountService.recharge(loginId, fee);
	}

}
